/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package rs232picjava;

import java.awt.Color;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Alan Carlos
 */
public class JanelaPrincipal extends javax.swing.JFrame {
    DefaultTableModel model;
    String columnNames[] = {"Sensores", "Status"};
    String data[][] = {
        {"Sensor 1", "Desligado"},
        {"Sensor 2", "Desligado"},
        {"Sensor 3", "Desligado"},
        {"Sensor 4", "Desligado"}
    };
    PortaSerial porta;
    /**
     * Creates new form JanelaPrincipal
     */
    public JanelaPrincipal() {
        initComponents();
        porta = new PortaSerial(this);
        model = new DefaultTableModel();
        setDisplay();
        
    
    }

    public void controlTabela(String dado) {
        int sensor;
   
        if(dado.equals("R")){ //alarme desativado
            for(int i=0; i<data.length;i++){ //até o número de linhas de data
             data[i][1] = "Desligado";
            }  
            display.setForeground(Color.black);
        }
        else{ //sensor ligado
        
        sensor =  Integer.parseInt(dado);
        this. data[sensor-1][1] = "Ativo";
        JOptionPane.showMessageDialog(null, "Sensor "+sensor+" Disparado","Atenção", JOptionPane.WARNING_MESSAGE);
        display.setForeground(Color.red);
        }
        setDisplay();
        
    }
    
    private void setDisplay(){
        model.setDataVector(data, columnNames);
        display.setModel(model);
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jMenuItem1 = new javax.swing.JMenuItem();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        imagem = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jTabbedPane2 = new javax.swing.JTabbedPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        display = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        newPassword = new javax.swing.JPasswordField();
        cadSenha = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();

        jMenuItem1.setText("jMenuItem1");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        imagem.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        imagem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/rs232picjava/labhard.png"))); // NOI18N
        getContentPane().add(imagem, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 180, 620, 300));
        imagem.getAccessibleContext().setAccessibleName("M");
        imagem.getAccessibleContext().setAccessibleDescription("");

        jLabel1.setText("jLabel1");
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(330, 400, -1, -1));

        display.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Sensor", "Status"
            }
        ));
        jScrollPane1.setViewportView(display);

        jTabbedPane2.addTab("Visão Geral", jScrollPane1);

        jLabel2.setText("Nova Senha");
        jPanel1.add(jLabel2);

        newPassword.setMinimumSize(new java.awt.Dimension(100, 20));
        newPassword.setPreferredSize(new java.awt.Dimension(100, 20));
        jPanel1.add(newPassword);

        cadSenha.setText("Cadastrar");
        cadSenha.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cadSenhaActionPerformed(evt);
            }
        });
        jPanel1.add(cadSenha);

        jTabbedPane2.addTab("Alterar Senha", jPanel1);

        getContentPane().add(jTabbedPane2, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 20, 560, 160));
        setJMenuBar(jMenuBar1);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cadSenhaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cadSenhaActionPerformed
         char s = '0';
        if(newPassword.getText().length()==4 && sohnumero(newPassword.getText()) && (!findChar(newPassword.getText(),'0'))){
            porta.writeSerial(newPassword.getText());
            JOptionPane.showMessageDialog(null, "Senha Cadastrada com Sucesso","Operação Finalizado com Sucesso", JOptionPane.INFORMATION_MESSAGE);
        }
        else{
             JOptionPane.showMessageDialog(null, "A senha deve possuir no máximo 4 caracteres de 1-9","Operação Cancelada", JOptionPane.INFORMATION_MESSAGE);
        }
    }//GEN-LAST:event_cadSenhaActionPerformed
    private boolean sohnumero(String senha){
           try{
               Integer.parseInt(senha);
           }
           catch(NumberFormatException e){
               return false;
           }
               
           
        return true;
        
    }
   private boolean findChar(String senha,char character){
       for(int i = 0; i < senha.length(); i++) {  
         if(senha.charAt(i) == character) { 
            return true;
         }  
         
       }
        return false;
   }
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cadSenha;
    private javax.swing.JTable display;
    private javax.swing.JLabel imagem;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTabbedPane jTabbedPane2;
    private javax.swing.JPasswordField newPassword;
    // End of variables declaration//GEN-END:variables
}
